package com.waterworld.watch.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.maps2d.model.Text;
import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.common.bean.NotificationCategoryBean;
import com.waterworld.watch.common.db.Notification;
import com.waterworld.watch.common.util.TimeUtils;
import com.waterworld.watch.mine.activity.NotificationActivity;
import com.waterworld.watch.push.contant.MessageContants;

import java.util.List;

/**
 * @author : Kris
 * @date : 2019/5/14 18:02
 */

public class NotificationCategoryAdapter extends RecyclerView.Adapter<NotificationCategoryAdapter.MyHolder> {

    public final static int EMERGENCY_REMIND = 1000;
    public final static int WATCH_HARDWARE = 1001;
    public final static int WATCH_FUNCTION = 1002;
    public final static int BINDING_SOCIAL = 1003;

    private Context mContext;
    private List<NotificationCategoryBean> mList;

    public NotificationCategoryAdapter(Context mContext, List mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.item_notification_category,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.time.setText(TimeUtils.getMonthAndDay(mList.get(i).getCreateTime()));
        JSONObject json = (JSONObject) JSONObject.parse(mList.get(i).getContent());
        switch (mList.get(i).getGroupId()) {
            case EMERGENCY_REMIND:
                Glide.with(mContext).load(R.drawable.ic_emergency_remind).into(myHolder.avatar);
                myHolder.category.setText(mContext.getString(R.string.emergency_remind));
                break;
            case WATCH_HARDWARE:
                Glide.with(mContext).load(R.drawable.ic_watch_hardware).into(myHolder.avatar);
                myHolder.category.setText(mContext.getString(R.string.watch_hardware));
                break;
            case WATCH_FUNCTION:
                Glide.with(mContext).load(R.drawable.ic_watch_function).into(myHolder.avatar);
                myHolder.category.setText(mContext.getString(R.string.watch_function));
                break;
            case BINDING_SOCIAL:
                Glide.with(mContext).load(R.drawable.ic_binding_social).into(myHolder.avatar);
                myHolder.category.setText(mContext.getString(R.string.binding_social));
                break;
        }
        switch (mList.get(i).getType()) {
            case MessageContants.WATCH_SHUTDOWN :
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_shut_down),mList.get(i).getWatchUserName(),mList.get(i).getWatchUserPhone().substring(7)));
                break;
            case MessageContants.WATCH_BIND:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_watch_bind),json.getString("phone"),mList.get(i).getWatchUserName(),json.getString("relationWithBaby")));
                break;
            case MessageContants.SECURITY_FENCE:
                if (json.getString("remindWay") != null) {
                    if (json.getString("remindWay").equals("1")) {
                        myHolder.content.setText(String.format(mContext.getString(R.string.notification_secret_fence), mList.get(i).getWatchUserName(), mList.get(i)
                                .getWatchUserPhone().substring(7), TimeUtils.getHourAndMin(json.getString("uploadTime")) + "进入", json.getString("fenceName")));
                    } else if (json.getString("remindWay").equals("2")) {
                        myHolder.content.setText(String.format(mContext.getString(R.string.notification_secret_fence), mList.get(i).getWatchUserName(), mList.get(i)
                                .getWatchUserPhone().substring(7), TimeUtils.getHourAndMin(json.getString("uploadTime")) + "离开", json.getString("fenceName")));
                    } else if (json.getString("remindWay").equals("3")) {
                        myHolder.content.setText(String.format(mContext.getString(R.string.notification_secret_fence), mList.get(i).getWatchUserName(), mList.get(i)
                                .getWatchUserPhone().substring(7), TimeUtils.getHourAndMin(json.getString("uploadTime")) + "未进入", json.getString("fenceName")));
                    }
                }
                break;
            case MessageContants.LOW_POWER:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_low_power),mList.get(i).getWatchUserName(),mList.get(i).getWatchUserPhone().substring(7)));
                break;
            case MessageContants.SIM_PULL_OUT:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_sim_pullout),mList.get(i).getWatchUserName(),mList.get(i).getWatchUserPhone().substring(7)));
                break;
            case MessageContants.URGENT_CALL:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_urgent_call),mList.get(i).getWatchUserName(),mList.get(i).getWatchUserPhone().substring(7),json.getString("urgentCall")));
                break;
            case MessageContants.WATCH_OFFLINE:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_watch_offline),mList.get(i).getWatchUserName(),mList.get(i).getWatchUserPhone().substring(7)));
                break;
        }
        myHolder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, NotificationActivity.class).putExtra("bean",mList.get(i)));
            }
        });


    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rl_parent)
        RelativeLayout parent;

        @BindView(R.id.iv_avatar)
        ImageView avatar;

        @BindView(R.id.tv_category)
        TextView category;

        @BindView(R.id.tv_time)
        TextView time;

        @BindView(R.id.tv_content)
        TextView content;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
